abstract class State {
    public abstract handle(): void
    public abstract transitionState(): void
}

class ConcreateState2 extends State {
    private context: Context

    constructor(context: Context) {
        super()
        this.context = context
    }

    //本状态下需要处理的逻辑
    public handle(): void {
        console.log('state2需要处理的逻辑');
    }

    //进行状态转移
    public transitionState(): void {
        this.context.currentState = this.context.STATE_1
        console.log("由 state2 转为 state1");
    }

}


class ConcreateState1 extends State {
    private context: Context

    constructor(context: Context) {
        super()
        this.context = context
    }

    //本状态下需要处理的逻辑
    public handle(): void {
        console.log('state1需要处理的逻辑');
    }

    //进行状态转移
    public transitionState(): void {
        this.context.currentState = this.context.STATE_2
        console.log("由 state1 转为 state2");
    }

}


class Context {
    //如果有多个状态则用列表存储
    public STATE_1: State = new ConcreateState1(this)
    public STATE_2: State = new ConcreateState2(this)
    public currentState: State

    constructor() {
        this.currentState = this.STATE_1
    }

    public handle() {
        this.currentState?.handle()
    }

    public transitionState() {
        this.currentState?.transitionState()
    }
}

const StateTest = () => {
    const context : Context = new Context()
    context.handle()
    context.transitionState()
    context.handle()
    context.transitionState()
    context.handle()
}

StateTest()